#ifndef GLOBALCONFIG_H
#define GLOBALCONFIG_H

#include <QString>
#include <QList>
#include <QStringList>
#include <QPoint>
#include <QDebug>
#include <QMap>
#include <QDateTime>
#include <time.h>
#include <QFile>
#include <QMutex>
#include "global_def.h"
#ifdef RUN_IN_BOARD
#include "M1515/hisi_dev.h"
#endif

enum FaultType{CurrentFault,HistoryFault};
enum FaultMode{DriveFault,MaintFault};
typedef struct SpecialMessageInfo
{
    uint id;
    QString name;
} SpecialMessageInfo;

typedef struct StationInfo
{
    QString name;
    uint stationId;
} StationInfo;

typedef struct LineInfo
{
    QString routeName;
    QString startName;
    QString destName;
    int stationCount;
    QList<StationInfo> station;
} LineInfo;
//------------------    通讯结构体   ---------------------------------
typedef struct TRDPSrcIPInfo
{
    QString srcip;
    bool xHBnormal;
    uint index;
    uint8_t HBcounter;
    uint16_t preHBvalue;
}TRDPSrcIPInfo;

typedef struct TRDPDataNodeInfo
{
    QString index;
    QString varName;
    unsigned int byte;
    unsigned char bit;
    unsigned char size;
    QString description;
} TRDPDataNodeInfo;

enum ObjType
{
    SEND = 0,
    RECV ,
};

typedef struct TRDPDataPortInfo
{
    uint comid;
    uint size;
    uint period;
    TRDPSrcIPInfo srcipinfo;
    QString destip;
    bool dduFlt;
    QString subsys;
    QString car;
    ObjType type;
    QList<TRDPDataNodeInfo> node;

} TRDPDataPortInfo;

//------------------    IO结构体   ---------------------------------
typedef struct InterfaceInfo
{
    uint id;
    int car;
    int index;
    QString description;
    QString pinId;
    QString sys;
    int value;
} InterfaceInfo;

//------------------    IO类型   ---------------------------------
enum IOStyle{AI,RIOM_DI_TC,LCU_DI_Tc,LCU_DI_Mp,LCU_DI_M,EVR_DI_M,LCU_DO,IO_TYPE_CNT};

typedef struct MVBDataNodeInfo
{
    QString index;
    QString varName;
    unsigned char byte;
    unsigned char bit;
    unsigned char size;
    QString description;
} MVBDataNodeInfo;

typedef struct MVBDataPortInfo
{
    uint port;
    uint size;
    uint period;
    QString source;
    QString sink;
    QList<MVBDataNodeInfo> node;
} MVBDataPortInfo;

typedef struct EventInfo
{
    uint id;
    QString car;
    QString description;
    QString level;
    QString hint;
    QString code;
} EventInfo;

typedef struct EventData
{
    EventInfo event;
    bool ack;
    time_t startTime;
    time_t endTime;
} EventData;

typedef struct FaultInfo
{
    int id;
    FaultMode mode;
    QString car;
    QString description;
    QString level;
    QString hint;
    QString code;
} FaultInfo;

typedef struct FaultData
{
    FaultInfo fault;
    bool ack;
    time_t startTime;
    time_t endTime;
} FaultData;

typedef struct
{
    uint id;
    QString time;
    QString car;
    QString description;
    QString level;
} AlarmInfo;


class GlobalConfig
{
public:
    GlobalConfig();
//------------------    通用配置文件   ---------------------------------
    static int cabNumber;
    static int cabActivated;
    static int trainNumber;
    static int trainSpeed;
    static bool ATCComFlt;
    static bool xLocalActive;
    static bool xFAM;
    static bool xZeroSpeed;
    static int PISMode;
    static bool ATPFS_Active;
    static int ATPFSStat[2];
    static QString password;
    static QString configFilePath;
    static int readConfigFile(QString path);
    static void writePasswd(QString passwd);
    static int getCurrentBoardId();
    static int highVoltage;
    static uint8_t runDirection;
//------------------    通讯配置文件    ---------------------------------
    static QList<TRDPDataPortInfo> recvTRDPList;
    static QList<TRDPDataPortInfo> sendTRDPList;
    static int readRecvTRDPDataFile(QString path);
    static int Handle_CFG_TRDPPort(const QStringList &strlist);
    static int Handle_CFG_TRDPNode(const QStringList &strlist);
    static int Handle_CFG_EVENT(const QStringList &strlist);
    static int Handle_CFG_IO(const QStringList &strlist);
    static int readSendTRDPDataFile(QString path);
    static int getRecvVarIndex(int comidIndex, uint varoffset);
//------------------    IO接口信息    ---------------------------------
    static QList<InterfaceInfo> interfaceList[IO_TYPE_CNT];
    static int getInterfaceInfo(int id, int value, InterfaceInfo &info);
//------------------    故障配置文件   ---------------------------------
    static QList<EventInfo> eventList;
    static QMutex eventDatalocker;
    static QList<EventData> eventData;
    static QList<EventData> historyData;
    static QList<FaultInfo> faultList;
    static QList<FaultData> currentFaultData;
    static QList<FaultData> pastFaultData;
    static int faultLevelCnts[3];//4 represent level types :1,2,3,i
    static int getEventInfo(uint id, EventInfo &info);
    static int addEvent(uint id, unsigned long value = 1);
    static int removeEvent(uint id);
    static void ackEvent(uint id);
    static int getEventData(int index, EventData &data);
    static int getLatestTopLevelEvent(EventData &data);
    static int getHistoryData(int index, EventData &data);
    static int getEventResetID(QString type);
    static void updateFaultLevelCnt(const EventData &data,bool xAdd);
    static int getHighestFaultLevel();
    static void RecordLastFaults(EventData info);
    static void ReadLast200Faults();
//----------------------    空调信息    ---------------------------------------
    static int  SaloonHVACTemp[SaloonCnt+1];//+1 represent the whole train
    static void setSalHVACTemp(uint8_t sal, int temp);
//------------------    乘客信息系统配置文件    ---------------------------------
    static QList<SpecialMessageInfo> spacialMessageList;
    static QList<LineInfo> stationList;
    static int readRouteFile(QString path);//reserved
    static int readSpecialMessageFile(QString path);
    static int getRouteIndexByName(QString name);
    static int getStationIndexByName(int route, QString name);
    static QString getStationNameByID(uint id);
//------------------    操作日志    ---------------------------------
    static QString operationFilePath;
    static void addOperationInfo(QString op, bool boot=false);
    static int parseOperationFile(QString path);
    static void InitDDULog();
    static void writeLog(QString log);
    static bool xlog_init_ok;
//------------------    ATC    ---------------------------------
    static int atp_EB_test_success_delay;
};

#endif // GLOBALCONFIG_H
